#!/bin/bash
# autobackux - Backup your files
# Jon Peeters - June 2011

# autobackux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

labels="Configuration Menus"
dirs_file_tmp="$dirs_file.tmp"
config_dir="config"
config_file="$config_dir/config"
config_file_tmp="$config_file.tmp"
dirs_file="$config_dir/dirs_backup"
lng_file="$config_dir/language"
key_default_dest="default_dest"
config_keys=( "$key_default_dest" )
media_dir="/media"
red_color="<span color=\"#960000\">"
green_color="<span color=\"#51a244\">"
font_zenity="<span font_family=\"monospace\">"
resources_dir="resources"
log_dir="log"
rsync_log="$log_dir/rsync.log"
SRC=2
DST=3
NME=1
DTE=0

initialize() {
	if [ ! -f "$lng_file" ]; then
		# Language of the operating system
		language="${LANG:0:2}"
		if [[ -z "$language" ]]; then # No specific language
			language="en"
		fi
		echo "$language" > "$lng_file"
	else
		language=$(cat "$lng_file")
	fi
	
	if [ ! -d "src" ]; then
		fatal_error "Cannot find source directory"
	fi
	for srcfile in $(ls "src"); do
		source "src/$srcfile"
		if [ $? -ne 0 ]; then
			# Translation is may not be done yet => no translation for this error
			fatal_error "Cannot load file src/$srcfile"
		fi
	done

	# Everything is loaded => initialization can start
	translate_labels "$language"
	init_config
	build_menus
}

init_config() {
	if [ ! -f "$config_file" ]; then
		create_config
	fi
	old_ifs=$IFS
	IFS="|"
	results=""
	while read line; do
		args=( $line )
		hput "${args[0]}" "${args[1]}"
	done < "$config_file"
	IFS=$old_ifs
}

build_menus() {
	main_menu=( "true" "$_mainmenu_start_backup_" "start_backup" "-" "$_mainmenu_cd_backup_" "backup_cdrom" 
			"-" "$_mainmenu_display_backup_" "display_backups" "-" "$_mainmenu_edit_config_" "edit_menu"
			"-" "$_mainmenu_language_" "change_language" )
	edit_menu=( "true" "$_editmenu_add_backup_" "add_backup" "-" "$_editmenu_mv_backup_" "move_backup" 
			"-" "$_editmenu_rm_backup_" "remove_backup" "-" "$_editmenu_edit_default_dir_" "edit_default" )
}

create_config() {
	if [ ! -d "$config_dir" ]; then
		mkdir "$config_dir"
		if [ $? -ne 0 ]; then
			fatal_error "$_config_cannot_create_dir_ $config_dir"
		fi
	fi	
	cd "$media_dir"
	default_dest=""
	while [ "$default_dest" == "" ]; do
		zenity --info --title="$_config_dflt_install_dir_title_" --text="$_config_dflt_install_dir_"
		default_dest=$(zenity --file-selection --directory --title="$_config_dflt_install_dir_title_")
	done
	cd -
	echo "$key_default_dest|$default_dest" > $config_file
}

persist_config() {
	rm -f $config_file_tmp 2>/dev/null
	for key in ${config_keys[@]}; do
		hget "$key"
		if [[ -n "$values" ]]; then
			echo "$key|$values" >> $config_file_tmp
		fi
	done
	mv $config_file_tmp $config_file
}

main_menu() {
	choice="-"
	while [[ -n "$choice" ]]; do	
		choice=$(zenity --title="$_main_title_" --text="$_main_menu_" \
				--width=500 --height=250 --list --print-column=3\
				--cancel-label="$_mainmenu_cancel_" \
				--radiolist --column="" --column="" --column="" --hide-column=3 "${main_menu[@]}")
		if [[ -z "$choice" ]]; then
			break
		fi
		$choice
	done
}

edit_menu() {
	choice2="-"
	while [[ -n "$choice2" ]]; do		
		choice2=$(zenity --title="$_editmenu_title_" --text="$_editmenu_" \
				--width=500 --height=210 --list --print-column=3 \
				--cancel-label="$_editmenu_cancel_" \
				--radiolist --column="" --column="" --column="" --hide-column=3 "${edit_menu[@]}")
		if [[ -z "$choice2" ]]; then
			break
		fi
		$choice2
	done
}

change_language() {
	if [[ -z "$lngmenustring" ]]; then	
		lngmenustring=""
		for file in $(ls "$resources_dir/labels_"*); do	
			thelng="${file##*_}"
			tmp=_mainmenu_language_"$thelng"_
			label=${!tmp}
			if [[ -z "$label" ]]; then
				continue
			fi
			lngmenustring="$lngmenustring - $label $thelng"
		done
		lngmenu=( $lngmenustring )
	fi
	choice3="-"
	choice3=$(zenity --title="$_lngmenu_title_" --text="$_lngmenu_" \
				--width=300 --height=120 --list --print-column=3 \
				--cancel-label="$_lngmenu_cancel_" \
				--radiolist --column="" --column="" --column="" --hide-column=3 "${lngmenu[@]}")
	if [[ -z "$choice3" ]]; then
		return
	fi
	language="$choice3"
	echo "$choice3" > "$lng_file"
	translate_labels "$language"
	build_menus
	lngmenustring=""
}

load_backups() {
	if [[ ! -f "$dirs_file" ]] || [[ -z $(cat "$dirs_file") ]]; then
		display_error "$_no_backup_defined_"
		return
	fi
	if [ "$1" != "force" ] && [ ${#backups[@]} -gt 0 ]; then 
		return # Already loaded
	fi
	i=0
	while read line; do
		backups[$i]="$line"
		echo "LOADING : ${backups[$i]}"
		((i++))
	done < "$dirs_file"
}

parseRecord() {
	old_ifs=$IFS
	IFS="|"
	values=( $1 )
	IFS=$old_ifs
}

parseAllRecords() {
	parseRecord $(printf "%s|" "${backups[@]}")
}

#== Custom hashmap (Associative array is not global)

hput () {
	eval hash"${1}"='$2'
}

hget () {
	values=$(eval echo '${hash'"$1"'#hash}')
}


